#include"LinpopHeaders.h"

struct sockaddr_in socklogin_send, socklogin_recv, socklmsg_send, socklmsg_recv;
gchar buf[1024];
gchar flags[1024];
gchar Login_userinfo[1024] = {0};
gchar msg_buf[1024] = {0};
userIP user_IP[1024];
userstate user_state[1024];
userIP get_user_IP;
userstate get_user_state;

int socket_login_send,socket_login_recv;
int socket_lmsg_send, socket_lmsg_recv;
int sock_len;
int ret;
int open_userinfo_fd, open_userstate_fd, open_lmsg_fd;
FILE *open_userinfo_fp, *open_userstate_fp, *open_lmsg_fp;
int test;




pthread_t Login_Listenthread_t;
pthread_t Mainframe_Listenthread_t;


clientRequest Get_client_request(char* buffer)
{
    clientRequest client_request;
    int num;
    int n;
    unsigned int i,j;
    char ch;
    char buf[BUFLEN] = {0};

    bzero(buf,sizeof(buf));
    n = 0;
    for(i=0,j=0; i<strlen(buffer); i++,j++)
    {
        ch = buffer[i];
        if(ch == '~')
        {
            switch(n)                                               //n代表~的个数，根据n将字符串buf存入client_requestzhong
            {
            case 0:
            {
                num = atoi(buf);
                client_request.requestType = num;
                bzero(buf,sizeof(buf));
                //
                break;
            }
            case 1:
            {
                bzero(client_request.senderName,NAMELEN);
                strcpy(client_request.senderName,buf);
                bzero(buf,sizeof(buf));
                //
                break;

            }
            case 2:
            {
                bzero(client_request.destName,NAMELEN);
                strcpy(client_request.destName,buf);
                bzero(buf,sizeof(buf));
                //
                break;
            }
            case 3:
            {
                bzero(client_request.senderPasswd,NAMELEN);
                strcpy(client_request.senderPasswd,buf);
                bzero(buf,sizeof(buf));
                //
                break;
            }
            case 4:
            {
                bzero(client_request.sourceIP,NAMELEN);
                strcpy(client_request.sourceIP,buf);
                bzero(buf,sizeof(buf));
                break;
            }
            case 5:
            {
                bzero(client_request.destIP,NAMELEN);
                strcpy(client_request.destIP,buf);
                bzero(buf,sizeof(buf));
                //
                break;
            }
            case 6:
            {
                bzero(client_request.nickName,NAMELEN);
                strcpy(client_request.nickName,buf);
                bzero(buf,sizeof(buf));
                //
                break;
            }
            case 7:
            {
                num = atoi(buf);
                client_request.state= num;
                bzero(buf,sizeof(buf));
                //
                break;
            }
            case 8:
            {
                bzero(client_request.gender,NAMELEN);
                strcpy(client_request.gender,buf);
                bzero(buf,sizeof(buf));

                //
                break;
            }
            case 9:
            {
                num = atoi(buf);
                client_request.age = num;
                bzero(buf,sizeof(buf));
                //                 printf("%d\n",num);
                break;
            }
            case 10:
            {
                bzero(client_request.branch,NAMELEN);
                strcpy(client_request.branch,buf);
                bzero(buf,sizeof(buf));
                //
                break;
            }
            case 11:
            {
                bzero(client_request.telephone,NAMELEN);
                strcpy(client_request.telephone,buf);
                bzero(buf,sizeof(buf));
                //
                break;
            }
            case 12:
            {
                bzero(client_request.birthdate,NAMELEN);
                strcpy(client_request.birthdate,buf);
                bzero(buf,sizeof(buf));
                //
                break;
            }
            case 13:
            {
                num = atoi(buf);
                client_request.profilepic = num;
                bzero(buf,sizeof(buf));
                break;
            }
            case 14:
            {
                bzero(client_request.msg,NAMELEN);
                strcpy(client_request.msg,buf);
                bzero(buf,sizeof(buf));
                break;
            }
            }

            num = 0;
            n++;
            j=-1;
        }
        else
        {
            buf[j] = ch;
        }

    }
 //   Print( client_request);
    return client_request;
}

userstate Get_userstate(char* buffer)
{
    userstate user_state;
    int num;
    int n;
    unsigned int i,j;
    char ch;
    char buf[BUFLEN] = {0};

    bzero(buf,sizeof(buf));
    n = 0;
    for(i=0,j=0; i<strlen(buffer); i++,j++)
    {
        ch = buffer[i];
        if(ch == '~')
        {
                switch(n)                                               //n代表~的个数，根据n将字符串buf存入client_requestzhong
                {
                        case 0:
                        {
                                break;
                        }
                        case 1:
                        {
                                bzero(get_user_state.Name,NAMELEN);
                                strcpy(get_user_state.Name,buf);
                                bzero(buf,sizeof(buf));
                                break;
                        }
                        case 2:
                        {
                                num = atoi(buf);
                                get_user_state.state = num;
                                bzero(buf,sizeof(buf));
                                break;
                        }
                        default:break;
            }

            num = 0;
            n++;
            j=-1;
        }
        else
        {
            buf[j] = ch;
        }

}

    return user_state;
}


void Get_userIP(char* buffer)
{

//    int num;
    int n;
    unsigned int i,j;
    char ch;
    char buf[BUFLEN] = {0};
    char string[BUFLEN] = {0};

    string[0] = '~';
    strcat(buffer,string);
    printf("buf     %s\n",buffer);

    bzero(buf,sizeof(buf));
    n = 0;
    for(i=0,j=0; i<strlen(buffer); i++,j++)
    {
        ch = buffer[i];
        if(ch == '~')
        {
                    switch(n)                                               //n代表~的个数，根据n将字符串buf存入client_requestzhong
                    {
                            case 0:
                            {
                                    break;
                            }
                            case 1:
                            {
                                bzero(get_user_IP.Name,NAMELEN);
                                strcpy(get_user_IP.Name,buf);
                                printf("user_IP.Name%s\n",get_user_IP.Name);
                                bzero(buf,sizeof(buf));
                                break;
                            }
                            case 2:
                            {
                                printf("caonima");
                                bzero(get_user_IP.IPaddress,16);
                                strcpy(get_user_IP.IPaddress,buf);
                                printf("buf:%s\n",buf);

                                printf("user_IP.IPaddress%s\n",get_user_IP.IPaddress);
                                bzero(buf,sizeof(buf));

                                break;

                            }
                            default:break;
                        }

//            num = 0;
            n++;
            j=-1;
        }
        else
        {
            buf[j] = ch;
        }

}

 //   return user_IP;
}


void *Login_Listenthread(void *arg)
{
         int j = 0;
         int i = 0;
         int k = 0;
        clientRequest tempinfo[10];
        bzero(tempinfo, sizeof(tempinfo));
        user_num=0;
        bzero(&socklogin_recv, sizeof(socklogin_recv));
        socklogin_recv.sin_family = AF_INET;
        socklogin_recv.sin_addr.s_addr = INADDR_ANY;
        socklogin_recv.sin_port = htons(PORT);
        sock_len = sizeof(socklogin_recv);
        if((socket_login_recv = socket(AF_INET,SOCK_DGRAM,0)) == -1)                                                                     //接受注册成功/失败信息
        {
                perror("Socket_recv");
                exit(1);
        }
        else if (bind(socket_login_recv,(struct sockaddr *)&socklogin_recv,sizeof(socklogin_recv)) == -1)
        {
                printf("Error in bind\n");
                exit(1);
                close(socket_login_send);
                close(socket_login_recv);
        }
        else
        {
                while(1)
                {
                        if (recvfrom(socket_login_recv,buf,sizeof(buf),0,(struct sockaddr*)&socklogin_recv, (socklen_t *)&sock_len) != -1)
                        {
                                g_print("buf:%s\n",buf);
                        //           g_print("%d\n",test);
                                if(strcmp(buf, "woca") != 0)
                                {


                                        if((strcmp(buf,"error") != 0) && (strcmp(buf,"Login_success") != 0) && (strcmp(buf,"login") != 0))
                                        {
                                                test = strcmp(buf,"0");
                                                g_print("%d\n",test);
                                                if(buf[0] == 's')
                                                {
                                     //                   g_print("%s\n",buf);
                                                        strcat(buf, "~");
                                                        g_print("state:%s\n",buf);

                                                        user_state[i] = Get_userstate(buf);

                                                        bzero(buf,sizeof(buf));
                                                        g_print("user_state[k].state:%d\n",user_state[i].state);
                                                        g_print("user_state[k].Name:%s\n",user_state[i].Name);
                                                        i++;


                                                }
                                                else if(buf[0] == 'I')
                                                {

                                                       Get_userIP(buf);
                                                       strcpy(user_IP[j].Name,get_user_IP.Name);
                                                       strcpy(user_IP[j].IPaddress,get_user_IP.IPaddress);

                                                        bzero(buf,sizeof(buf));

                                                        g_print("user_IP[j].Name:%s\n",user_IP[j].Name);
                                                        g_print("user_IP[j].IPaddress:%s\n",user_IP[j].IPaddress);
                                                        j++;



                                                }
                                                else if(buf[0] == 'M')
                                                {

                                                        tempinfo[k] = Get_client_request(buf);
                                                        strcpy(chat_msg[k].Name, tempinfo[k].senderName);
                                                        strcpy(chat_msg[k].msg, tempinfo[k].msg);
                                                        bzero(buf,sizeof(buf));
                                                        g_print("user_IP[j].Name:%s\n",chat_msg[k].Name);
                                                        g_print("user_IP[j].IPaddress:%s\n",chat_msg[k].msg);
                                                        k++;
                                                }
                                                else if((open_userinfo_fd = open("./userinfo.txt",O_CREAT|O_RDWR,0777)) != -1)
                                                {
                                                        close(open_userinfo_fd);
                                                        userinfo[user_num] = Get_client_request(buf);
                                                        user_num++;
                                                        bzero(buf,sizeof(buf));
        /*                                              if((open_userinfo_fp = fopen("./userinfo.txt","a+")) != NULL)
                                                        {
                                                                fseek(open_userinfo_fp,-1,SEEK_CUR);
                                                                fputs(buf,open_userinfo_fp);
                                                                bzero(buf,sizeof(buf));
                                                                buf[0] = '\n';
                                                                fputs(buf,open_userinfo_fp);
                                                                fclose(open_userinfo_fp);
                                                       }
        */                                        }
                                        }
                                        else
                                        {
                                                strcat(flags,buf);
                                                bzero(buf,sizeof(buf));
                                                //                 g_print("%s\n",buf);
                                                g_print("flags:%s\n",flags);
                                        }
                                }
                                else
                                {
                                        break;
                                }
                        }
                }
        }
        return (void *)0;
}

void on_Login_Window_destroy_event(GtkObject *object,gpointer user_data)
{
      struct sockaddr_in sock_offline;
        int socket_offline;
        gchar offline_buf[1024];
        bzero(offline_buf,1024);

        offline_buf[0] = '5';
        strcat(offline_buf,"~");                                                                                                                                //组包
        //Username
        strcat(offline_buf,myinformation.senderName);
        strcat(offline_buf,"~");
        //destName
        strcat(offline_buf,"~");
        //Passwd
        strcat(offline_buf,"~");
        strcat(offline_buf,myinformation.sourceIP);
        //sourceIP
        strcat(offline_buf,"~");
        //destIP

        strcat(offline_buf,"~");
        //nickname
        strcat(offline_buf,"~");
        //state
        strcat(offline_buf,"~");
        //Gender
        strcat(offline_buf,"~");
        //Age
        strcat(offline_buf,"~");
        //branch
        strcat(offline_buf,"~");
        //tel
        strcat(offline_buf,"~");
        //birth
        strcat(offline_buf,"~");
        //profilepic
        strcat(offline_buf,"~");
        //msg
        strcat(offline_buf,"~");


        bzero(&sock_offline,sizeof(sock_offline));
        sock_offline.sin_family = AF_INET;
        sock_offline.sin_addr.s_addr = inet_addr(SERVERIP);
        sock_offline.sin_port = htons(PORT);
        sock_len = sizeof(sock_offline);
        if((socket_offline = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
        {
                perror("Socket_send");
                exit(1);
        }

        if((ret = sendto(socket_offline,offline_buf,1024,0,(struct sockaddr*)&sock_offline,sock_len)) == -1)
        {
                perror("Send to");
                exit(1);
        }
        g_print("Login destroy!\n");
        gtk_main_quit();
}



int main(int argc, char *argv[])
{
        FILE *open_userpasswd_fp ;
        char ch = ' ';
        int i = 0, j = 0;
        gchar Username_buf[NAMELEN];
        gchar Passwd_buf[NAMELEN];

        bzero(Username_buf, NAMELEN);
        bzero(Passwd_buf, NAMELEN);
        //mydrawimage = 0;//绘画窗口没打开
        bzero(user_window,sizeof(user_window));
        bzero(chat_msg, sizeof(chat_msg));
        gtk_init(&argc, &argv);
        Login_builder = gtk_builder_new();
        gtk_builder_add_from_file(Login_builder,"Login.glade",NULL);

        Login_Window = GTK_WIDGET(gtk_builder_get_object(Login_builder,"Login_Window"));

        Login_UsernameEntry = GTK_WIDGET(gtk_builder_get_object(Login_builder,"Login_UsernameEntry"));
        Login_PasswdEntry = GTK_WIDGET(gtk_builder_get_object(Login_builder,"Login_PasswdEntry"));
        gtk_entry_set_visibility(GTK_ENTRY(Login_PasswdEntry),FALSE);


        Login_RegisterButton = GTK_WIDGET(gtk_builder_get_object(Login_builder,"Login_RegisterButton"));
        Login_QuitButton = GTK_WIDGET(gtk_builder_get_object(Login_builder,"Login_QuitButton"));
        Login_LoginButton = GTK_WIDGET(gtk_builder_get_object(Login_builder,"Login_LoginButton"));
        Login_CheckButton = GTK_WIDGET(gtk_builder_get_object(Login_builder,"Login_CheckButton"));

        g_signal_connect(G_OBJECT(Login_RegisterButton),"clicked",G_CALLBACK(on_Login_RegisterButton_clicked),NULL);
        g_signal_connect(G_OBJECT(Login_QuitButton),"clicked",G_CALLBACK(on_Login_QuitButton_clicked),NULL);
        g_signal_connect(G_OBJECT(Login_LoginButton),"clicked",G_CALLBACK(on_Login_LoginButton_clicked),NULL);
        g_signal_connect(G_OBJECT(Login_Window),"delete_event",G_CALLBACK(on_Login_Window_destroy_event),NULL);

        if((open_userpasswd_fp = fopen("./userpasswd.txt","rb")) != NULL)
        {
                fseek(open_userpasswd_fp, 0 , SEEK_SET);
                ch = fgetc(open_userpasswd_fp);
                g_print("ch:%c\n",ch);
                if(ch == '1')
                {
                        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(Login_CheckButton), TRUE);
                        ch = fgetc(open_userpasswd_fp);
                        g_print("ch:%c\n",ch);
                        ch = fgetc(open_userpasswd_fp);
                        while(ch != '\n')
                        {
                                Username_buf[i] = ch;
                                ch = fgetc(open_userpasswd_fp);
                                i++;
                        }
                        g_print("USername buf:%s\n",Username_buf);
                        ch = fgetc(open_userpasswd_fp);
                        while(ch != '\n')
                        {
                                Passwd_buf[j] = ch;
                                ch = fgetc(open_userpasswd_fp);
                                j++;
                        }
                        gtk_entry_set_text(GTK_ENTRY(Login_UsernameEntry), Username_buf);
                        gtk_entry_set_text(GTK_ENTRY(Login_PasswdEntry), Passwd_buf);


                }


        }
        fclose(open_userpasswd_fp);

        gtk_builder_connect_signals(Login_builder,NULL);
        g_object_unref(G_OBJECT(Login_builder));
        gtk_widget_show(Login_Window);
        gtk_main();


        return 0 ;

}

void on_MainWindow_destroy_event(GtkObject *object, gpointer user_data)
{
     struct sockaddr_in sock_offline;
        int socket_offline;
        gchar offline_buf[1024];
        bzero(offline_buf,1024);

        offline_buf[0] = '5';
        strcat(offline_buf,"~");                                                                                                                                //组包
        //Username
        strcat(offline_buf,myinformation.senderName);
        strcat(offline_buf,"~");
        //destName
        strcat(offline_buf,"~");
        //Passwd
        strcat(offline_buf,"~");
        strcat(offline_buf,myinformation.sourceIP);
        //sourceIP
        strcat(offline_buf,"~");
        //destIP

        strcat(offline_buf,"~");
        //nickname
        strcat(offline_buf,"~");
        //state
        strcat(offline_buf,"~");
        //Gender
        strcat(offline_buf,"~");
        //Age
        strcat(offline_buf,"~");
        //branch
        strcat(offline_buf,"~");
        //tel
        strcat(offline_buf,"~");
        //birth
        strcat(offline_buf,"~");
        //profilepic
        strcat(offline_buf,"~");
        //msg
        strcat(offline_buf,"~");


        bzero(&sock_offline,sizeof(sock_offline));
        sock_offline.sin_family = AF_INET;
        sock_offline.sin_addr.s_addr = inet_addr(SERVERIP);
        sock_offline.sin_port = htons(PORT);
        sock_len = sizeof(sock_offline);
        if((socket_offline = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
        {
                perror("Socket_send");
                exit(1);
        }

        if((ret = sendto(socket_offline,offline_buf,1024,0,(struct sockaddr*)&sock_offline,sock_len)) == -1)
        {
                perror("Send to");
                exit(1);
        }
        g_print("mainfraim destroy!\n");
        gtk_main_quit();

}
void on_Login_LoginButton_clicked(GtkObject *object, gpointer user_data)
{

    FILE *open_userpasswd_fp;


    gchar imagepath[20];
    close(socket_login_recv);
    Login_UsernameEntry_char =NULL;
    Login_PasswdEntry_char = NULL;

    Login_UsernameEntry_char = gtk_entry_get_text(GTK_ENTRY(Login_UsernameEntry));
    Login_PasswdEntry_char = gtk_entry_get_text(GTK_ENTRY(Login_PasswdEntry));
    g_print("%s\n",Login_UsernameEntry_char);


    if((open_userpasswd_fp = fopen("./userpasswd.txt","wb")) != NULL)
    {
        fseek(open_userpasswd_fp, 0, SEEK_SET);
        if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (Login_CheckButton)))
        {
            fputs("1", open_userpasswd_fp);
            fputs("\n", open_userpasswd_fp);
            fputs(Login_UsernameEntry_char,open_userpasswd_fp);
            fputs("\n", open_userpasswd_fp);
            fputs(Login_PasswdEntry_char,open_userpasswd_fp);
            fputs("\n", open_userpasswd_fp);
        }
        else
        {
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (Login_CheckButton), FALSE);
            fputs("0", open_userpasswd_fp);
        }
    }
    fclose(open_userpasswd_fp);

    bzero(buf, sizeof(buf));
    bzero(Login_userinfo,sizeof(Login_userinfo));
    Login_userinfo[0] = '0';

    strcat(Login_userinfo,"~");
    g_print("%s\n",Login_userinfo);
    strcat(Login_userinfo,Login_UsernameEntry_char);
    strcat(Login_userinfo,"~");
        //
    strcat(Login_userinfo,"~");
    strcat(Login_userinfo,Login_PasswdEntry_char);
    strcat(Login_userinfo,"~");

    int i;
    for(i = 0; i < 11; i ++)
    {
        strcat(Login_userinfo,"~");
    }

    g_print("%s\n",Login_userinfo);
    bzero(&socklogin_send,sizeof(socklogin_send));
    socklogin_send.sin_family = AF_INET;
    socklogin_send.sin_addr.s_addr = inet_addr(SERVERIP);
    socklogin_send.sin_port = htons(9741);
    sock_len = sizeof(socklogin_send);
    if((socket_login_send = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
    {
                perror("Socket_send");
                exit(1);
        }

        if((ret = sendto(socket_login_send,Login_userinfo,sizeof(Login_userinfo),0,(struct sockaddr*)&socklogin_send,sock_len)) == -1)
        {
                perror("Send to");
                exit(1);
        }
        else
        {
                close(socket_login_send);
                pthread_create(&Login_Listenthread_t, NULL, Login_Listenthread,NULL);
                int i = 0;
                for(i = 0; i < 200; i++)
                {
                        if(flags[0] == '\0')
                        {
                                g_print("bieshuile\n");
                                usleep(10000);
                        }
                        else
                        {
                                break;
                        }
                }

                usleep(500000);
                if(flags[0] == '\0')
                {
                    //弹出服务器error窗口
                        pthread_cancel(Login_Listenthread_t);
                        close(socket_login_recv);
                        Servererror_builder = gtk_builder_new();
                        gtk_builder_add_from_file(Servererror_builder,"Servererror.glade",NULL);
                        Servererror_Window = GTK_WIDGET(gtk_builder_get_object(Servererror_builder,"Servererror_Window"));
                        Servererror_Button = GTK_WIDGET(gtk_builder_get_object(Servererror_builder,"Servererror_Button"));
                        g_signal_connect(G_OBJECT(Servererror_Button),"clicked",G_CALLBACK(on_Servererror_Button_clicked),NULL);
                        gtk_builder_connect_signals(Servererror_builder,NULL);
                        g_object_unref(G_OBJECT(Servererror_builder));
                        gtk_widget_show(Servererror_Window);
                        gtk_main();

                        g_print("Server Error!\n");

                }
                else
                {
                        close(socket_login_recv);
                        pthread_cancel(Login_Listenthread_t);
                        if(strcmp(flags,"Login_success") == 0)
                        {
                            bzero(flags,sizeof(flags));
                                //跳转到主界面
                                int i = 0;
                             //   int j;
                                for(i = 0; i < user_num; i++)
                                {
                                    if(strcmp(userinfo[i].senderName,userinfo[i+1].senderName) == 0)
                                    {
                                        user_num --;
                                        bzero(userinfo[i+1].senderName,NAMELEN);
                                        printf("用户名有重复:%s \n",userinfo[i].senderName);
                                    }
                                        g_print("userinfo.requestType:%d\n",userinfo[i].requestType);
                                        g_print("userinfo.senderName:%s\n",userinfo[i].senderName);
                                        g_print("userinfo.destName:%s\n",userinfo[i].destName);
                                        g_print("userinfo.senderPasswd:%s\n",userinfo[i].senderPasswd);
                                        g_print("userinfo.sourceIP:%s\n",userinfo[i].sourceIP);
                                        g_print("userinfo.destIP:%s\n",userinfo[i].destIP);
                                        g_print("userinfo.nickName:%s\n",userinfo[i].nickName);
                                        g_print("userinfo.state:%d\n",userinfo[i].state);
                                        g_print("userinfo.gender:%s\n",userinfo[i].gender);
                                        g_print("userinfo.age:%d\n",userinfo[i].age);
                                        g_print("userinfo.branch:%s\n",userinfo[i].branch);
                                        g_print("userinfo.telephone:%s\n",userinfo[i].telephone);
                                        g_print("userinfo.birthdate:%s\n",userinfo[i].birthdate);
                                        g_print("userinfo.profilepic:%d\n",userinfo[i].profilepic);
                                        g_print("userinfo.msg:%s\n",userinfo[i].msg);



                                }
                                int j;
                                for(j = 0;j < user_num; j++)
                                {
                                    strcpy(user_window[j].Name,userinfo[j].senderName);
                                    user_window[j].flag = 0;
                                }

                                int k = 0;
                                int l = 0;
                                while(userinfo[k].senderName[0] != '\0')
                                {
                                        l = 0;
                                        while(user_IP[l].Name[0] != '\0')
                                        {
                                                if(strcmp(user_IP[l].Name, userinfo[k].senderName) == 0)
                                                {
                                                        g_print("Same IP:%s", user_IP[l].IPaddress);
                                                        strcpy(userinfo[k].sourceIP, user_IP[l].IPaddress);
                                                }
                                                l++;
                                        }
                                        k++;
                                }



                                GtkWidget *myface;
                                GtkWidget *Mainframe_HeadButton;
                                GtkWidget *mygroup_btn;
                                GtkWidget *Mainframe_UsernameLabel;
                                gchar MyFacePath[30];
                                gtk_widget_hide(Login_Window);
                                bzero(MyFacePath,sizeof(MyFacePath));
                                Mainframe_builder = gtk_builder_new();
                                gtk_builder_add_from_file(Mainframe_builder,"Mainframe.glade",NULL);

                                Mainframe_Window =  GTK_WIDGET(gtk_builder_get_object(Mainframe_builder,"Mainframe_Window"));
                                Mainframe_SettingButton =  GTK_WIDGET(gtk_builder_get_object(Mainframe_builder,"Mainframe_SettingButton"));
                                Mainframe_HeadButton =  GTK_WIDGET(gtk_builder_get_object(Mainframe_builder,"Mainframe_HeadButton"));
                                mygroup_btn =  GTK_WIDGET(gtk_builder_get_object(Mainframe_builder,"Mainframe_GroupButton"));
                                Mainframe_UsernameLabel = GTK_WIDGET(gtk_builder_get_object(Mainframe_builder,"Mainframe_UsernameLabel"));




                                scrolledwindow1 = GTK_WIDGET(gtk_builder_get_object(Mainframe_builder,"scrolledwindow1"));
                                viewport1 = GTK_WIDGET(gtk_builder_get_object(Mainframe_builder,"viewport1"));

                                fixed = gtk_fixed_new();
                                gtk_container_add (GTK_CONTAINER (viewport1), fixed);
                                i=0;
                                //myinformation = {0};
                                memset(&myinformation,0,sizeof(myinformation));
                                while((i < user_num) && (userinfo[i].senderName[0] != '\0'))
                                {
                                    g_print ("user_num : %d",user_num);
                                    btn1[i] = gtk_button_new();
                                    gtk_fixed_put(GTK_FIXED(fixed), btn1[i], (20+160*(i%3)), (10+80*(i/3)));
                                    gtk_widget_set_size_request(btn1[i], 60, 60);
                                     sprintf(imagepath,"./LittleFace/%d.bmp",userinfo[i].profilepic);
                                    image1[i] = gtk_image_new_from_file(imagepath);
                                    g_print("imagepath : %s\n",imagepath);
                                //    gtk_widget_show (image1[j+3*i]);
                                    gtk_container_add (GTK_CONTAINER (btn1[i]), image1[i]);
                                    label1[i] = gtk_label_new(userinfo[i].senderName);
                                    gtk_fixed_put(GTK_FIXED(fixed), label1[i], (80+160*(i%3)), (10+80*(i/3)));
                                    gtk_widget_set_size_request(label1[i], 100, 60);
                                    if(strcmp(userinfo[i].senderName,Login_UsernameEntry_char) ==0)
                                        myinformation = userinfo[i];
                                    g_signal_connect(G_OBJECT(btn1[i]),"clicked",G_CALLBACK(show_conversation_window),&userinfo[i]);
                                    i++;
                                }
                            sprintf(MyFacePath,"./NormalFace/%d.bmp",myinformation.profilepic);
                            myface = gtk_image_new_from_file(MyFacePath);
                            gtk_widget_show (myface);
                            gtk_container_add (GTK_CONTAINER (Mainframe_HeadButton), myface);
                            g_signal_connect(G_OBJECT(Mainframe_SettingButton),"clicked",G_CALLBACK(on_Mainframe_SettingButton_clicked),NULL);
                            g_signal_connect(G_OBJECT(Mainframe_Window),"delete_event",G_CALLBACK(on_MainWindow_destroy_event),NULL);
                            g_signal_connect(G_OBJECT(mygroup_btn),"clicked",G_CALLBACK(on_MygroupBtn_clicked),NULL);
                            g_signal_connect(G_OBJECT(Mainframe_HeadButton),"clicked",G_CALLBACK(show_pesornal_info),&myinformation);
                            gtk_label_set_text(GTK_LABEL(Mainframe_UsernameLabel),myinformation.senderName);
                            pthread_create(&Mainframe_Listenthread_t, NULL, Mainframe_Listenthread,NULL);


                            gtk_builder_connect_signals(Mainframe_builder,NULL);
                            g_object_unref(G_OBJECT(Mainframe_builder));
                            gtk_widget_show_all(Mainframe_Window);
                            gtk_main();


                        }
                        else if(strcmp(flags,"error") == 0)
                        {
                                bzero(flags,sizeof(flags));
                                //弹出登陆失败窗口
                                Loginerror_builder = gtk_builder_new();
                                gtk_builder_add_from_file(Loginerror_builder,"Loginerror.glade",NULL);
                                Loginerror_Window = GTK_WIDGET(gtk_builder_get_object(Loginerror_builder,"Loginerror_Window"));
                                Loginerror_Button = GTK_WIDGET(gtk_builder_get_object(Loginerror_builder,"Loginerror_Button"));
                                g_signal_connect(G_OBJECT(Loginerror_Button),"clicked",G_CALLBACK(on_Loginerror_Button_clicked),NULL);
                                gtk_builder_connect_signals(Loginerror_builder,NULL);
                                g_object_unref(G_OBJECT(Loginerror_builder));
                                gtk_widget_show(Loginerror_Window);
                                gtk_main();

                        }
                        else if(strcmp(flags,"login") == 0)
                        {
                                //弹出重复登录窗口
                                bzero(flags,sizeof(flags));
                                Reregister_builder = gtk_builder_new();
                                gtk_builder_add_from_file(Reregister_builder,"Reregister.glade",NULL);
                                Reregister_Window = GTK_WIDGET(gtk_builder_get_object(Reregister_builder,"Reregister_Window"));
                                Reregister_Button = GTK_WIDGET(gtk_builder_get_object(Reregister_builder,"Reregister_Button"));

                                g_signal_connect(G_OBJECT(Reregister_Button),"clicked",G_CALLBACK(on_Reregister_Button_clicked),NULL);
                                gtk_builder_connect_signals(Reregister_builder,NULL);
                                g_object_unref(G_OBJECT(Reregister_builder));
                                gtk_widget_show(Reregister_Window);
                                gtk_main();
                        }
                }

        }
}


void on_Login_QuitButton_clicked(GtkObject *object,gpointer user_data)
{
        g_print("login_mainfraim destroy!\n");
        gtk_main_quit();

}



void on_Login_RegisterButton_clicked(GtkObject *object,gpointer user_data)
{
        gtk_widget_hide(Login_Window);
        Register_builder = gtk_builder_new();
        gtk_builder_add_from_file(Register_builder,"Register.glade",NULL);

        Register_Window = GTK_WIDGET(gtk_builder_get_object(Register_builder,"Register_Window"));
        Psderror_Window = GTK_WIDGET(gtk_builder_get_object(Register_builder,"Psderror_Window"));

        Register_CancelButton = GTK_WIDGET(gtk_builder_get_object(Register_builder,"Register_CancelButton"));
        Register_ConfirmButton = GTK_WIDGET(gtk_builder_get_object(Register_builder,"Register_ConfirmButton"));
        Register_UsernameEntry = GTK_WIDGET(gtk_builder_get_object(Register_builder,"Register_UsernameEntry"));
        Register_PasswdEntry = GTK_WIDGET(gtk_builder_get_object(Register_builder,"Register_PasswdEntry"));
        Register_PsdconfEntry = GTK_WIDGET(gtk_builder_get_object(Register_builder,"Register_PsdconfEntry"));
        Register_AgeEntry = GTK_WIDGET(gtk_builder_get_object(Register_builder,"Register_AgeEntry"));
        Register_GenderCombox = GTK_WIDGET(gtk_builder_get_object(Register_builder,"Register_GenderCombox"));
        Register_BranchCombox = GTK_WIDGET(gtk_builder_get_object(Register_builder,"Register_BranchCombox"));

        Register_NicknameEntry = GTK_WIDGET(gtk_builder_get_object(Register_builder,"Register_NicknameEntry"));
        Register_BirthEntry = GTK_WIDGET(gtk_builder_get_object(Register_builder,"Register_BirthEntry"));
        Register_TelephoneEntry = GTK_WIDGET(gtk_builder_get_object(Register_builder,"Register_TelephoneEntry"));

        gtk_entry_set_visibility(GTK_ENTRY(Register_PasswdEntry),FALSE);
        gtk_entry_set_visibility(GTK_ENTRY(Register_PsdconfEntry),FALSE);

        g_signal_connect(G_OBJECT(Register_ConfirmButton),"clicked",G_CALLBACK(on_Register_ConfirmButton_clicked),NULL);
        g_signal_connect(G_OBJECT(Register_CancelButton),"clicked",G_CALLBACK(on_Register_CancelButton_clicked),NULL);

        gtk_builder_connect_signals(Register_builder,NULL);
        g_object_unref(G_OBJECT(Register_builder));
        gtk_widget_show(Register_Window);
        gtk_main();
}


